'''
第八周作业第二题,抓取京东购物车信息
使用python程序进行模拟登录并且记录cookie,在抓取购物车信息时候将cookie运用到该功能中
目前由于出现拼图验证码 暂时无办法识别 所以此处取的cookie为登录后的cookie

时间:20180918
爬取地址 : https://cart.jd.com/cart.action
'''

# 导入爬取数据所需模块
from pyquery import PyQuery as pq
import requests
import re,time,json,os

# 定义url 信息
_url = "https://cart.jd.com/cart.action"

# 封装头信息
headers = {
	'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
	'accept-language':'zh-CN,zh;q=0.9',
	'cache-control':'max-age=0',
	'cookie':'__jdu=1531463767740533647783; shshshfpb=2ee9daa04a1434ef687c5573268444cf35b4008237e8064b39da848582; shshshfpa=8eaf4492-5504-7a3f-7d88-1da818b30489-1532488114; __jdc=122270672; PCSYCityID=1; ipLoc-djd=1-72-2799-0; __jdv=122270672|direct|-|none|-|1536905754863; __jda=122270672.1531463767740533647783.1531463768.1537174287.1537240029.12; pin=%E9%BD%90%E5%BE%B7%E9%BE%99%E4%B8%9C%E5%BC%BA074; _tp=6yOWlSEjCvwTDRQJJ7%2Bdcz0I1jSIcEwJvNDn5ZYcr3uqyqjeq7qyXtcLf3XC1JiMqHtvCDaMqdoYs5iv4xnerA%3D%3D; _pst=%E9%BD%90%E5%BE%B7%E9%BE%99%E4%B8%9C%E5%BC%BA074; unick=%E9%BD%90%E5%BE%B7%E9%BE%99%E4%B8%9C%E5%BC%BA074; pinId=Y3NBteTdxhrl7Yb6NTzpeuwsc6ZiR5tl; user-key=b21e30cc-d52e-43fd-bfce-e9e70d6b26ce; TrackID=1rcV0h0qBE1xyxj2PcKrve8dGbPikIHwN9Bi77KvQLNoP7DgBvF5XhngcW0xyE_C6w1Qzhrx59g3gB5TRoKJXJSe9-N_EuWD5Ek3Nv4Ewhlc; ceshi3.com=000; thor=1EB8881D89C3D8003E07F2FD6AD482F69B3C258E5D4A76AC5440E747D4695E4F8D8A8297657A43908DA43F2D74ADB2A481C380029F5BC9980159FFCC48DDA7ABF572EA6F5E8931677883DB15B7D0190481E4E67E8B2C9ADFC74E52A65687CCA881783F9C97E8B294081DB5FBD3362D3C65062C35B63FCA0E4811019C879F01D9191F0703991418911511F0604A6888AF; cart-main=xx; 3AB9D23F7A4B3C9B=XA76N34JDSLEO3PXVDJ66LLKAI4MDS27APZU4HFDGZLFKLMPAIMOAC7FJUDOCLHLZNQV7NUR6RT7BC6RQKL5QOPPQI; cn=6; cd=0; shshshfp=e74302c871be0874b735d8b6ba831aa1; shshshsID=fc052575720b140f3aca97ee7af538b2_9_1537241086751; __jdb=122270672.24.1531463767740533647783|12.1537240029',
	'referer':'https://cart.jd.com/addToCart.html?rcd=1&pid=7306981&pc=1&eb=1&rid=1537241064464&em=',
	'upgrade-insecure-requests':'1',
	'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}



# 获取页面信息
res = requests.get(_url,headers=headers)
if res.status_code == 200:
	html = res.text
	# print(len(html))
	# =========使用pyquery解析==================
	# 解析HTML文档
	document = pq(html)
	items = document("div.item-selected")

	goods_list = []
	pic_list = []
	for item in items.items():
		img_src = 'https:'+item.find("div.item-form div.p-goods div.goods-item div.p-img a img").attr('src')
		pic_list.append(img_src)
		data = {
			'image':img_src,
			'goods_name':item.find("div.item-form div.p-goods div.goods-item div.p-name a").text(),
			'price':item.find("div.item-form div.p-sum strong").text()
		}

		goods_list.append(data)

	print(pic_list)


	# 定义文件目录
	goods_result = "./jd/"
	# 判断文件是否存在,存在则进行移除,否则创建
	if os.path.exists(goods_result):
		thumb = os.listdir(goods_result)
		for y in thumb:
			os.remove(goods_result+y)
	else:
		os.mkdir(goods_result)

	# 图片处理
	goods_img_path = "./jd_img/"
	# 判断文件是否存在,存在则进行移除,否则创建
	if os.path.exists(goods_img_path):
		thumb = os.listdir(goods_img_path)
		for y in thumb:
			os.remove(goods_img_path+y)
	else:
		os.mkdir(goods_img_path)

	# 下载图片
	x = 0
	for pic in pic_list:
		# print('开始下载第'+str(x+1)+'张图片')
		pres = requests.get(pic)
		# 以二进制写入的方式打开文件
		_img = open(goods_img_path+str(x+1)+'.jpg','wb')
		# 写入文件
		_img.write(pres.content)
		# 关闭文件
		_img.close()
		print('第'+str(x+1)+'张图片下载完成...')
		x+=1

	# 将结果写入到文本中
	txt = open(goods_result+'cart_goods.txt','w',encoding="utf-8")
	for goods in goods_list:
		txt.write(json.dumps(goods, ensure_ascii=False) + '\n')
	txt.close
	print('文本写入成功')

else:
	print('暂无信息...')	



